package com.zerdoor.controller;

import com.google.gson.Gson;
import com.zerdoor.model.VipSet;
import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.VipSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2015/1/29.
 */
@Controller
@RequestMapping("vipSet")
public class VipSetController {
    @Autowired
    public VipSetService vipSetService;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){
        return "/vipSet/view";
    }

    /**
     * 查询树形列表（用户等级）
     * @param id 用户等级父类编号
     * @param request
     * @param page 当前页
     * @param rows 页大小
     * @return
     */
    @RequestMapping(value = "view", method = RequestMethod.POST)
    @ResponseBody
    public String view(int id,HttpServletRequest request,int page,int rows){
        if(id == 0){
            PageHelper.startPage(page, rows);
            List<Map<String, Object>> list = vipSetService.getVipSetInfo(id);
            PageInfo pageInfo = new PageInfo(list);
            Gson gson = new Gson();
            Map<String,Object> obj = new HashMap<String, Object>();
            obj.put("rows", getJson(list));
            obj.put("total", pageInfo.getTotal());
            String result = gson.toJson(obj);
            return result;
        }else{
            //添加子节点
            List<Map<String, Object>> list = vipSetService.getVipSetInfo(id);
            Gson gson = new Gson();
            return gson.toJson(getJson(list));
        }
    }

    /**
     * 初始化list列表
     * @param list
     * @return
     */
    public List<VipSet> getJson(List<Map<String, Object>> list){
        List<VipSet> list2 = new ArrayList<VipSet>();
        for (Map<String, Object> temp : list){
            VipSet v = new VipSet();
            v.setId(Integer.parseInt(temp.get("id").toString()));
            v.setNameCn(temp.get("nameCn").toString());
            v.setNameEn(temp.get("nameEn").toString());
            v.setSort(Integer.parseInt(temp.get("sort").toString()));
            v.setStartTrading(Integer.parseInt(temp.get("startTrading").toString()));
            v.setEndTrading(Integer.parseInt(temp.get("endTrading").toString()));
            v.setLevels(Integer.parseInt(temp.get("levels").toString()));
            v.setParentId(Integer.parseInt(temp.get("parentId").toString()));
            list2.add(v);
        }
        return  list2;
    }

    /**
     * 添加用户等级
     * @param request
     * @return
     */
    @RequestMapping(value = "addVipSet", method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> addType(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("level_id", null);
        map.put("nameCn", request.getParameter("nameCn"));
        map.put("nameEn", request.getParameter("nameEn"));
        map.put("parent", Integer.parseInt(request.getParameter("parent")));
        map.put("sort", Integer.parseInt(request.getParameter("sort")));
        map.put("startTrading", Integer.parseInt(request.getParameter("startTrading")));
        map.put("endTrading", Integer.parseInt(request.getParameter("endTrading")));
        map.put("levels", Integer.parseInt(request.getParameter("levels")));
        try {
            vipSetService.addVipSet(map);
        } catch (Exception e) {
            map.put("errormsg",e.getMessage());
        }
        return map;
    }

    /**
     * 编辑用户等级
     * @param request
     * @return
     */
    @RequestMapping(value = "editVipSet", method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> editVipSet(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String,Object>();

        map.put("levelId", Integer.parseInt(request.getParameter("levelId")));
        map.put("nameCn", request.getParameter("nameCn"));
        map.put("nameEn", request.getParameter("nameEn"));
        map.put("sort", Integer.parseInt(request.getParameter("sort")));
        map.put("startTrading", Integer.parseInt(request.getParameter("startTrading")));
        map.put("endTrading", Integer.parseInt(request.getParameter("endTrading")));
        try {
            vipSetService.updateVipSet(map);
        } catch (Exception e) {
            map.put("errormsg",e.getMessage());
        }
        return map;
    }

    /**
     * 删除用户等级
     * @param request
     * @return
     */
    @RequestMapping(value = "deleteVipSet", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteVipSet(HttpServletRequest request,int levelId){
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            vipSetService.deleteVipSet(levelId);
        } catch (Exception e) {
            map.put("errormsg",e.getMessage());
        }
        return map;
    }
}
